import java.util.Scanner;

public class ReplaceableTree extends StringTree {
	
	public ReplaceableTree (String node){
		super(node);
	}
	public ReplaceableTree (){
		super();
	}

	@Override
	public String search(String node) {	
		String result = depthSearch(node, rootNode);
        if (result == null) {
			return "Node not found"; 
		}
        return result;
	}
	
	@Override
	public void add(String node) {
		if(this.rootNode==null) {
			this.rootNode = new StringNode(node);
		} else {
			Queue Q = new Queue();
			Q.push(rootNode);
			boolean notFound = true;
			while (notFound) {
				StringNode currentNode = Q.pop();
				if (currentNode.getLeftChild() == null) {
						currentNode.setLeftChild(new StringNode(node));
						notFound = false;
				} else if (currentNode.getRightChild() == null) {
						currentNode.setRightChild(new StringNode(node));
						notFound = false;
				} else {
//					if (currentNode.getLeftChild() != null) {
						Q.push(currentNode.getLeftChild());
//					}else if(currentNode.getRightChild() != null){	 //shitti stuff
						Q.push(currentNode.getRightChild());
//					}
				}
			}
		}
	}
	
		
	public void replace(String position, String subTree) {
		StringNode toAdd = rootNode;
		Scanner s = new Scanner(position).useDelimiter(" ");
		String temp;
		while(s.hasNext()) {
				temp = s.next();
				if(temp.equals("left")) {
						toAdd = toAdd.getLeftChild();
				} else if(temp.equals("right")) {
						toAdd = toAdd.getRightChild();
				}
				System.out.println(temp);
		}
		toAdd = this.createSubTree(subTree);
		//missing
	}
	
	private StringNode createSubTree(String subTree) {
		Scanner s = new Scanner(subTree).useDelimiter("\n");
		String currentline = "";
		if(s.hasNext()) {
			currentline = s.next();		
		}
		currentline=currentline.trim();
		currentline = currentline.substring(4);
	
		return null;
	}
	
	private String depthSearch(String wantedNode, StringNode currentNode) {
		if (currentNode == null) {
				return null;
		}
		String curStr = currentNode.getLabel();
		if (curStr.equals(wantedNode)) {
				return "";
		}
		String resultleft = depthSearch(wantedNode, currentNode.getLeftChild());
		String resultright = depthSearch(wantedNode, currentNode.getRightChild());
		if (!(resultleft == null)) {
				return "left " + resultleft;
		}
		if (!(resultright == null)) {
				return "right " + resultright;
		}
		return null;    
	}
}
